package com.itheima.tanhua.exception;

import com.itheima.tanhua.vo.ErrorResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Lff
 * @date 2022/3/24
 * 统一的异常处理类
 */
@RestControllerAdvice
public class ExceptionAdvice {

    /**
     * 处理用户业务异常
     * @param e
     */
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity businessException(BusinessException e) {
        e.printStackTrace();
        ErrorResult errorResult = e.getErrorResult();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity businessException(Exception e) {
        e.printStackTrace();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.error());
    }
}
